package com.keqing.tomato.state;

import android.app.Service;
import android.os.Vibrator;
import android.view.View;
import android.widget.Button;

import com.keqing.tomato.fragment.FragmentTomatoTime;
import com.keqing.tomato.util.TimeUtil;

import java.util.Date;
import java.util.Objects;

public abstract class State {

    public static final int begin_focus = 1;
    public static final int focusing = 2;
    public static final int finish_focus = 3;
    public static final int begin_rest = 4;
    public static final int resting = 5;
    public static final int finish_rest = 6;

    protected FragmentTomatoTime fragment;

    public void setContext(FragmentTomatoTime fragment) {
        this.fragment = fragment;
    }

    //刷新当前页面
    public abstract void initPage();

    //点击开始专注
    public abstract void clickBeginFocus();

    //点击开始休息
    public abstract void clickBeginRest();

    //计时结束后，处理方法
    public abstract void doTimerFinish();

    //点击重置
    public abstract void clickReset();

    //点击计时模式
    public abstract void clickTimeMode();

    protected void setPageContent(String text, Boolean deadTimeVisible, String deadTime, Button visibleButton) {
        if (text != null) {
            fragment.tvTime.setText(text);
        }
        if (deadTimeVisible != null) {
            if (deadTimeVisible) {
                fragment.tvEndTime.setVisibility(View.VISIBLE);
                if (fragment.tvEndTime != null) {
                    fragment.tvEndTime.setText(deadTime);
                }
            } else {
                fragment.tvEndTime.setVisibility(View.INVISIBLE);
            }
        }
        if (visibleButton != null) {
            setButtonVisible(visibleButton);
        }
    }

    protected void playVibration() {
        if (fragment.mySetting.isVibration()) {
            Vibrator vibrator = (Vibrator) Objects.requireNonNull(fragment.getActivity()).getSystemService(Service.VIBRATOR_SERVICE);
            vibrator.vibrate(new long[]{50, 500, 100, 500}, -1);
        }
    }

    private void setButtonVisible(Button button) {
        fragment.beginFocusBtn.setVisibility(View.GONE);
        fragment.focusingBtn.setVisibility(View.GONE);
        fragment.beginRestBtn.setVisibility(View.GONE);
        fragment.restingBtn.setVisibility(View.GONE);
        button.setVisibility(View.VISIBLE);
    }

    String historyTitle(Date now) {
        String title = "";
        int hour = TimeUtil.getHour(now);
        if (hour <= 6) {
            title = "夜半三更，学习不辍";
        } else if (hour <= 11) {
            title = "一日之计在于晨";
        } else if (hour <= 15) {
            title = "正午时光，学习不误";
        } else if (hour <= 18) {
            title = "人约黄昏后";
        } else if (hour <= 24) {
            title = "夜幕降临，无人打扰";
        }
        return title;
    }

}
